package hot;

import java.util.HashSet;
import java.util.Set;

public class Num128 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int i : nums) {
            set.add(i);
        }
        int maxlength = 0;
        for(int i : set) {
            if(!set.contains(i - 1)) {
                int num = i;
                int length = 1;
                while(set.contains(num + 1)) {
                    num += 1;
                    length += 1;
                }
                maxlength = Math.max(maxlength,length);
            }
        }
        return maxlength;
    }
}
